home *** CD-ROM | disk | FTP | other *** search
/ Die Speccy' 97 / Die Speccy' 97.iso / amiga_system / the_aminet / comm / bbs / s342q07.lha / arch.c < prev    next >
C/C++ Source or Header  |  1994-08-02  |  4KB  |  187 lines

  1. /*
  2. *                Arch.c
  3. * Archival functions.
  4. */
  5. #include "ctdl.h"
  6. /*
  7. *                            history
  8. *
  9. * 89Jan09 HAW  Completed initial version.
  10. */
  11. /*
  12. *                            Contents
  13. *
  14. *    AskForNSMap()        search function
  15. *    ChkNtoStr()             testing function for archival stuff
  16. *    ChkStrtoN()             checks strings against each other
  17. *    EatNMapStr()            eat an archival string.
  18. *    FreeNtoStr()            frees a data node of archive type
  19. *    NtoStrInit()            utility initialization function
  20. *    WrtNtoStr()             writes to ctdlarch.sys
  21. */
  22. /*
  23. * Arch_base
  24. *
  25. * This list contains the list of archive files for archive rooms.  The
  26. * information consists of the room slot number, the string representing
  27. * the filename to place messages in, and the max size of the file.
  28. */
  29. SListBase  Arch_base =
  30.   {
  31.   NULL, ChkNtoStr, NULL, FreeNtoStr, EatArchRec
  32.  
  33.   };
  34. /*
  35. * EatNMapStr()
  36. *
  37. * This will eat a string formatted as "<digit> <string>".  It is used as
  38. * part of a list to read information in from text files that is in a
  39. * generic format and place it in a multipurpose data structure.
  40. */
  41. void *EatNMapStr(char *line)
  42.   {
  43.   char    *s;
  44.   if ((s = strchr(line, ' ')) == NULL)
  45.   return NULL;
  46.   return NtoStrInit(atoi(line), s+1, 0, FALSE);
  47.  
  48.   }
  49. /*
  50. * EatArchRec()
  51. *
  52. * This function creates an archive record from the line passed in.
  53. */
  54. void *EatArchRec(char *line)
  55.   {
  56.   char *s, *c;
  57.   if ((s = strchr(line, ' ')) == NULL)
  58.   return NULL;
  59.   if ((c = strchr(s + 1, ' ')) == NULL)
  60.   return NtoStrInit(atoi(line), s+1, 0, FALSE);
  61.   *c++ = 0;
  62.   return NtoStrInit(atoi(line), s+1, atoi(c), FALSE);
  63.  
  64.   }
  65. /*
  66. * NtoStrInit()
  67. *
  68. * This is a utility function to generate temporary or permanent data nodes
  69. * for searching or adding to Archive-type lists.
  70. */
  71. void *NtoStrInit(int num, char *str, int num2, char needstatic)
  72.   {
  73.   NumToString *temp;
  74.   static NumToString t =
  75.     {
  76.     0, NULL
  77.  
  78.     };
  79.   if (needstatic)
  80.     {
  81.     temp = &t;
  82.     if (temp->string != NULL) free(temp->string);
  83.  
  84.     }
  85.   else
  86.   temp = (NumToString *) GetDynamic(sizeof *temp);
  87.   temp->num  = num;
  88.   temp->num2 = num2;
  89.   temp->string = strdup(str);
  90.   return (void *) temp;
  91.  
  92.   }
  93. static char ReturnNum2 = FALSE;
  94. /*
  95. * GetArchSize()
  96. *
  97. * This function gets the requested size of the archive.  This is acquired
  98. * from the list Arch_base.  If the archive can't be found (never happens)
  99. * then 0 is returned.
  100. */
  101. int GetArchSize(int num)
  102.   {
  103.   UNS_16 *x;
  104.   ReturnNum2 = TRUE;
  105.   x = SearchList(&Arch_base, NtoStrInit(num, "", 0, TRUE));
  106.   ReturnNum2 = FALSE;
  107.   if (x == NULL) return 0;
  108.   return (int) *x;
  109.  
  110.   }
  111. /*
  112. * ChkNtoStr()
  113. *
  114. * This function is used to search lists which use the NumToString lists.  It
  115. * searches using the numerical value as a key.
  116. */
  117. void *ChkNtoStr(NumToString *d1, NumToString *d2)
  118.   {
  119.   if (d1->num == d2->num) return (ReturnNum2) ? &d1->num2 : d1->string;
  120.   return NULL;
  121.  
  122.   }
  123. /*
  124. * ChkStrtoN()
  125. *
  126. * This function is used to search lists which use the NumToString lists.  It
  127. * searches using the string value as a key.
  128. */
  129. void *ChkStrtoN(NumToString *d1, char *string)
  130.   {
  131.   if (strCmpU(d1->string, string) == 0)
  132.   return &d1->num;
  133.   else return NULL;
  134.  
  135.   }
  136. /*
  137. * WrtNtoStr()
  138. *
  139. * This function is used to generically update files with the information
  140. * contained in NumToString structures.  NB: FILE variable upfd must be opened
  141. * by the user.
  142. */
  143. void WrtNtoStr(NumToString *d)
  144.   {
  145.   extern FILE *upfd;
  146.   fprintf(upfd, "%d %s\n", d->num, d->string);
  147.  
  148.   }
  149. /*
  150. * WrtArchRec()
  151. *
  152. * This function writes an archive record out.  NB: FILE variable upfd must be
  153. * opened by the user.
  154. */
  155. void WrtArchRec(NumToString *d)
  156.   {
  157.   fprintf(upfd, "%d %s %d\n", d->num, d->string, d->num2);
  158.  
  159.   }
  160. /*
  161. * FreeNtoStr()
  162. *
  163. * This function frees a general use NumToString node.  We use this rather
  164. * than free because the field string is also dynamically allocated.
  165. */
  166. void FreeNtoStr(NumToString *d)
  167.   {
  168.   free(d->string);
  169.   free(d);
  170.  
  171.   }
  172. /*
  173. * AskForNSMap()
  174. *
  175. * This function satisfies a request for a search of a list for the map value
  176. * of a given integer.  If fails, returns pointer to a zero length string.
  177. */
  178. char *AskForNSMap(SListBase *base, int val)
  179.   {
  180.   char *check;
  181.   static char *zero = "";
  182.   if ((check = (char *) SearchList(base, NtoStrInit(val, "", 0, TRUE))) != NULL)
  183.   return check;
  184.   return zero;
  185.  
  186.   }
  187.